<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="50%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> 扫码图纸添加 </span>
    </template>
    <BasicForm @register="registerForm" />
    <XzddForm @register="registerXzddDrawer" @success="handleSuccess" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsM8OrderOrderForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed,onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner, useDrawer } from '/@/components/Drawer';
  import { m8Rd32ScanPicAddRow } from '/@/api/m8/rd/rd32/m8Rd32';
  import XzddForm from './formXzdd.vue';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('setting.pricemodel.wecharPriceModelInv');
  const { showMessage } = useMessage();
  const record = ref<WecharPriceModelUse>({} as WecharPriceModelUse);
  const getTitle = computed(() => ({
    icon: 'i-ant-design:book-outlined',
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: '',
      field: 'selIds',
      component: 'InputTextArea',
      required: true,
      componentProps: {
        placeholder: '请先输入或扫描图纸',
        rows: 32,
        allowClear: true,
      },
      // componentProps: ({ tableAction, formModel }) => {
      //   return {
      //     placeholder: '请先输入或扫描图纸',
      //     maxlength: 1000,
      //     rows: 32,
      //     onblur: (e) => {
      //       let value = e.target.value;
      //       let newValue = value.replace(/\t/g, '@@');
      //       formModel['selIds'] = newValue;
      //     }
      //   }
      // },
      required: true,
    },
  ];

  
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerXzddDrawer, { openDrawer }] = useDrawer();
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    // const res = await wecharPriceModelUseForm(data);
    // record.value = (res.wecharPriceModelUse || {}) as WecharPriceModelUse;
    record.value = data
    record.value.__t = new Date().getTime();
    // setFieldsValue(record.value);
    document.getElementById('form_item_selIds')?.focus();
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {

    const data = await validate();
    console.log(data);
    
    try {
      const data = await validate();
      data.cusCode = record.value.cusCode
      setDrawerProps({ confirmLoading: true });
      const res = await m8Rd32ScanPicAddRow(data);
      if(res.data.okNum == res.data.list.length){
        let arr = res.data.list.map(item=>{
          return item.orderPic
        })
        showMessage(res.message);
        setTimeout(closeDrawer);
        emit('success',arr);
      }else{
        openDrawer(true,res.data);
      }
      return false;
      // showMessage(res.message);
      // setTimeout(closeDrawer);
      // let obj = res.data;
      // let arr = [];
      // for (const key in obj) {
      //     obj[key].map((item) => {
      //       arr.push(item);
      //     });
      // }
      // emit('success', arr);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }

  function handleSuccess(selectData) {
    setTimeout(closeDrawer);
    emit('success', selectData);
  }
</script>
